﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BusinessLogic;

namespace Website
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindGrid();

        }
        /// <summary>
        /// Fires when page links are clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ChangePage(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            // Refresh the list
            BindGrid();
        }

        /// <summary>
        /// Fires when Columns heading are clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SortRecords(object sender, GridViewSortEventArgs e)
        {
            DataTable dataTable = GridDataSource();

            if (dataTable != null)
            {
                DataView dataView = new DataView(dataTable);

                dataView.Sort = GetSortExpression(e);

                GridView1.DataSource = dataView;
                GridView1.DataBind();
            }
        }

        #region Private Methods

        /// <summary>
        /// Bind the gridview
        /// </summary>
        private void BindGrid()
        {
            GridView1.DataSource = GridDataSource();
            GridView1.DataBind();
        }

        /// <summary>
        /// Get GridView DataSource
        /// </summary>
        private DataTable GridDataSource()
        {
            BookManager p = new BookManager();
            DataTable dTable = new DataTable();
            try
            {
                dTable = p.Load();
            }
            catch (Exception ee)
            {
             //   lblMessage.Text = ee.Message.ToString();
            }
            finally
            {
                p = null;
            }

            return dTable;
        }

        /// <summary>
        /// Get sort expression for the gridview
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        private string GetSortExpression(GridViewSortEventArgs e)
        {
            string sortDirection = string.Empty;
            // if clicked on the same column twice then let it toggle the sort order, else reset to ascending
            if (ViewState["SortExpression"] != null)
            {
                if (!ViewState["SortExpression"].ToString().Equals(e.SortExpression.ToLower()))
                {
                    ViewState["SortDirection"] = null;
                }
            }

            if (ViewState["SortDirection"] != null)
            {
                if (ViewState["SortDirection"].ToString().Equals("ASC"))
                {
                    sortDirection = "DESC";
                    ViewState["SortDirection"] = "DESC";
                }
                else
                {
                    sortDirection = "ASC";
                    ViewState["SortDirection"] = "ASC";
                }
            }
            else
            {
                ViewState["SortDirection"] = "ASC";
            }
            ViewState["SortExpression"] = e.SortExpression.ToLower();

            return e.SortExpression + " " + sortDirection;
        }
        #endregion Private Methods
    }
}
